package com.lq.javawebTest.controller;

import com.lq.JDBC.Dao.UserDao;
import com.lq.JDBC.Dao.impl.UserDaoImpl;
import com.lq.JDBC.entity.User;
import com.lq.javawebTest.core.entity.HttpRequest;
import com.lq.javawebTest.core.entity.HttpResponse;
import com.lq.javawebTest.core.inter.Servlet;

import java.sql.Date;


public class RegisterServlet implements Servlet {
    @Override
    public void service(HttpRequest request, HttpResponse response) {
        try{
            String username = request.getParameter("username");

            String password = request.getParameter("password");

            UserDao userDao = new UserDaoImpl();
//            int i = 1/0;
            User user = userDao.queryByName(username, User.class);
            //判断是否已经存在这个用户名
            if( user == null ){
                userDao.insert(new User(null,username,password,new Date(new java.util.Date().getTime())));
                HttpResponse.success(response, "恭喜你注册成功！！！");
            }else{
                HttpResponse.error(response, "注册失败，此用户名存在！！！");
            }
        }catch(Exception e){
            e.printStackTrace();
            HttpResponse.fail(response,"注册发生异常！！！");
        }

    }
}
